package cool.tianyou.blue.bird.rest;

import cool.tianyou.blue.bird.common.model.entity.RestApiResult;
import cool.tianyou.blue.bird.common.model.po.Device;
import cool.tianyou.blue.bird.common.model.po.LoginUser;
import cool.tianyou.blue.bird.common.tool.UserHolder;
import cool.tianyou.blue.bird.common.util.RestResultUtil;
import cool.tianyou.blue.bird.model.ChangeDeviceVO;
import cool.tianyou.blue.bird.module.synchronize.service.DeviceService;
import java.util.List;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Liutianyou
 * @date 2022/2/7 5:05 下午
 */
@RestController
@RequiredArgsConstructor
public class DeviceController {

  private final DeviceService deviceService;

  @GetMapping("/device")
  public ResponseEntity<RestApiResult<List<Device>>> userDeviceList() {
    LoginUser loginUser = UserHolder.getLoginUser();
    return RestResultUtil.ok(deviceService.getDeviceByUserId(loginUser.getUserId()));
  }

  @PutMapping("/device/alias")
  public ResponseEntity<RestApiResult<Object>> changeAlias(@RequestBody ChangeDeviceVO vo) {
    LoginUser loginUser = UserHolder.getLoginUser();
    deviceService.changeAlias(vo.getDeviceId(), vo.getAlias(), loginUser.getUserId());
    return RestResultUtil.ok();
  }

  @DeleteMapping("/device/{id}")
  public ResponseEntity<RestApiResult<Object>> delete(@PathVariable("id") Integer deviceId) {
    LoginUser loginUser = UserHolder.getLoginUser();
    deviceService.delete(deviceId, loginUser.getUserId());
    return RestResultUtil.ok();
  }

  @PostMapping("/device/offline/{id}")
  public ResponseEntity<RestApiResult<Object>> offline(@PathVariable("id") Integer deviceId) {
    LoginUser loginUser = UserHolder.getLoginUser();
    deviceService.offLineDevice(deviceId, loginUser.getUserId());
    return RestResultUtil.ok();
  }

  @PostMapping("/device/request/unlock")
  public ResponseEntity<RestApiResult<Object>> requestUnlock() {
    LoginUser loginUser = UserHolder.getLoginUser();
    deviceService.requestUnlock(loginUser.getDeviceId());
    return RestResultUtil.ok();
  }


  @PutMapping("/device/status")
  public ResponseEntity<RestApiResult<Object>> changeStatus(@RequestBody ChangeDeviceVO vo) {
    LoginUser loginUser = UserHolder.getLoginUser();
    deviceService.toggleStatus(vo.getDeviceId(), loginUser.getUserId(), vo.getLock());
    return RestResultUtil.ok();
  }


  @GetMapping("/device/pushable")
  public ResponseEntity<RestApiResult<Object>> pushableDevice() {
    LoginUser loginUser = UserHolder.getLoginUser();
    return RestResultUtil.ok(deviceService.findByUserId(loginUser.getUserId()));
  }

  @PutMapping("/device/push/state")
  public ResponseEntity<RestApiResult<Object>> pushableDevice(
      @RequestBody ChangeDeviceVO pushState) {
    LoginUser loginUser = UserHolder.getLoginUser();
    deviceService.changeDevicePushState(loginUser.getDeviceId(), pushState.getPushState());
    return RestResultUtil.ok();
  }


  @PostMapping("/device/key")
  public ResponseEntity<RestApiResult<Object>> updateKey(
      @RequestBody Map<String, String> publicKey) {
    LoginUser loginUser = UserHolder.getLoginUser();
    return RestResultUtil
        .ok(deviceService.updatePublicKey(loginUser.getDeviceId(), publicKey.get("publicKey")));
  }

}
